import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;

import javax.swing.JFileChooser;

//
//  @ Project : Project1
//  @ File Name : GameController.java
//  @ Date : 1/21/2013
//  @ Author : Graham Tapscott
//
//



/***
 * GameController - this class is responsible for registering and handling events from the UI
 * and dispatching the events to the GameModel class 
 * @author gtapscott
 *
 */
public class GameController {

	// Instance variables
	private GameModel _model;
	private GameView _view;

	/***
	 * GameController - represents a controller for a GameModel and GameView. 
	 * @param model
	 * @param view
	 */
	public GameController(GameModel model, GameView view) {
		_model = model;
		_view  = view;

		// Add the action listeners to the view
		view.addSearchBtnListener(new SearchBtnListener());
		view.addFileChooserListener(new FileChooserListener());	
	}

	/***
	 * SearchBtnListener - inner class used to implement the ActionListener for the searchBtn in the GameView
	 * 
	 */
	class SearchBtnListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			_model.setSearchText(_view.getUserInput());
			String userInput = _model.getSearchText();
			_model.setSelectedIndex(_view.getSearchType());
			if(userInput.length()==0){
				_view.showError("The search string is blank.");
				return;
			}		
			try {
				switch (_model.getSelectedIndex()){
				case 0: {
					int index = Integer.parseInt(userInput);					
					_view.setResults(((Game)_model).findObjByIndex(index));                	
				}
					break;
				case 1: {
					_view.setResults(((Game)_model).findObjByName(userInput));                	
				}
					break;
				case 2: {
					_view.setResults(((Game)_model).findObjByType(userInput));                	
				}
					break;
				default:
					break;                
				}

			} catch (NumberFormatException nfex) {
				_view.showError("Bad search term: '" + userInput + "', input String must be an integer");
			}
			catch (NullPointerException npex){
				_view.showError("Game file not loaded yet, please select a game file to load before searching");
			}		
		}
	}


	/***
	 * FileChooserListener - inner class used to implement the ActionListener for the searchBtn in the view
	 * 
	 */
	class FileChooserListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			//Handle open button action.
			if (e.getActionCommand() == "Select File") {
				// open the file chooser dialog box
				int returnVal = _view.openFileChooserDialog();

				// dispatch the action to the GameModel
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					File file = _view.getFileName();
					try {
						((Game)_model).loadWorld(file.toString());
					}
					// handle file not found
					catch (FileNotFoundException ex) {
						_view.showError("File does not exist: "+ file.toString());
					}					
					// handle number format exceptions
					catch (NumberFormatException ex){
						_view.showError("The file "+ file.toString() + " contains a malformed line.  Game load has stopped");
						
					}
					// Set UI elements 
					_view.setResults(((Game)_model).dumpAllObjects());
					_model.setSelectedfile(file.toString());
				}
			}        	 
		}
	}	

}
